/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.caracol.db.AccesoDatos;
import com.caracol.db.CaracolQueryGenerator;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Represent a row from the table in the database and contains all the basic functions (CRUD)
 * @author Roquet
 */
public class Arma {
    
    /**
     * Name of the table in the database
     */
    private static final String TABLE = "arma";
    
    /**
     * Name of the id column in the database;
     */
    private static final String ID = "id";
    
    /**
     * Name of the nombre column in the database
     */
    private static final String NOMBRE = "nombre";
    
    /**
     * Name of the calibre column in the database
     */
    private static final String CALIBRE = "calibre";
    
    /**
     * Name of the marca column in the database
     */
    private static final String MARCA = "marca";
    
    /**
     * Name of the modelo column in the database
     */
    private static final String MODELO = "modelo";
    
    /**
     * Name of the matricula column in the database
     */
    private static final String MATRICULA = "matricula";
    
    /**
     * Name of the id oficial column in the database
     */
    private static final String ID_OFICIAL = "id_oficial";
    
    /**
     * Identifier of this entity in the database
     */
    private int id;
    
    /**
     * Name of this "arma" (weapon)
     */
    private String nombre;
    
    /**
     * This is the mark of this "arma"
     */
    private String marcar;
    
    /**
     * This is the caliber of the "arma"
     */
    private String calibre;
    
    /**
     * Arma's model
     */
    private String modelo;
    
    /**
     * matricula (serial number) of this arma
     */
    private String matricula;
    
    /**
     * this is the identifier of the ofical that is owner of this weapon
     */
    private int idOficial;
    
    /**
     * This is the class that makes operations agains the database
     */
    private static CaracolQueryGenerator caracolQuery = new CaracolQueryGenerator();
    
    /**
     * Creates an instance of this class this contructor setting all the values
     * @param id this is the identifier od this element in the database
     * @param nombre name of the arma entity
     * @param calibre calibre of the arma entity
     * @param marca marca of the arma entity
     * @param modelo model of the arma entity
     * @param matricula matricula of the arma entity
     * @param idOficial identifier of the oficial at witch belongs this arma
     */
    public Arma(int id, String nombre, String calibre, String marca, String modelo, String matricula, int idOficial){
        this.id = id;
        this.nombre = nombre;
        this.calibre = calibre;
        this.marcar = marca;
        this.modelo = modelo;
        this.matricula = matricula;
        this.idOficial = idOficial;
    }

    /**
     *  Creates an instacen of this class with the default values
     */
    public Arma() {
        this(0, "", "", "", "", "", 0);
    }
    /**
     * Get's a arma entity from the database by search using it's identifier
     * @param id Ideintifier of this entity
     * @return Arma instance with the values from the database if the identifier
     * it's not found returns null
     * @throws SQLException if some database exception happend during the execution of this operation
     * @throws Exception if some exception happend during the execution of this operation
     */
    public static Arma getArma(int id) throws SQLException, Exception{
        Arma arma = null;
        String[] columns = new String[] { ID, NOMBRE, CALIBRE, MARCA, MODELO, MATRICULA, ID_OFICIAL };
        
        Map<String, String> where = new LinkedHashMap<String, String>();
        where.put(ID, new Integer(id).toString());
        
        List<Object> rawArma = caracolQuery.query(columns, TABLE, where, null, null, null, null);
        
        if (rawArma != null && rawArma.size() > 0) {
            List<Object> row = (List<Object>) rawArma.get(0);
            arma  = new Arma();
            arma.id = id;
            arma.setNombre(row.get(1).toString());
            arma.setCalibre(row.get(2).toString());
            arma.setMarcar(row.get(3).toString());
            arma.setModelo(row.get(4).toString());
            arma.setMatricula(row.get(5).toString());
            arma.setIdOficial(new Double(row.get(6).toString()).intValue());
        }
        return arma;
    }
    
    /**
     * Get's all the arma entitys in found in the database table
     * @return <code>List</code> containing all the rows in the table of the database
     * @throws SQLException if some database exception happend during the execution of this operation
     * @throws Exception if some exception happend during the execution of this operation
     */
    public static List<Arma> getAll() throws SQLException, Exception{
        ArrayList<Arma> armas = new ArrayList<Arma>();
        String[] columns = new String[] { ID, NOMBRE, CALIBRE, MARCA, MODELO, MATRICULA, ID_OFICIAL };
        List<Object> rawCargo = caracolQuery.query(columns, TABLE, null, null, null, null, null);
        
        for (Object object : rawCargo) {
            List<Object> row = (List<Object>) object;
            Arma arma = new Arma();
            arma.id = new Double(row.get(0).toString()).intValue();
            arma.setNombre(row.get(1).toString());
            arma.setCalibre(row.get(2).toString());
            arma.setMarcar(row.get(3).toString());
            arma.setModelo(row.get(4).toString());
            arma.setMatricula(row.get(5).toString());
            arma.setIdOficial(new Double(row.get(6).toString()).intValue());
            armas.add(arma);
        }
        
        return armas;
    }
    
    /**
     * Saves the values of this instance in the database
     * @return true if the operations completes successfully otherwise returns false
     * @throws SQLException if some database exception happends during the excecution of this operation
     * @throws Exception if some exception happends during the excecution of this operation
     */
    public boolean save() throws SQLException, Exception {
        if (getId() > 0) { // this is already save no need to continue
            return true;
        }
        Map<String,Object> values = new LinkedHashMap<String,Object>();
        values.put(NOMBRE, getNombre());
        values.put(CALIBRE, getCalibre());
        values.put(MARCA, getMarcar());
        values.put(MODELO, getModelo());
        values.put(MATRICULA, getMatricula());
        values.put(ID_OFICIAL, getIdOficial());
        
        int rowsAffected = caracolQuery.insert(TABLE, values);
        
        ArrayList<Object> data = null;
        if (rowsAffected > 0) {
            AccesoDatos ad = caracolQuery.getOpenConnection();
            data = new ArrayList<Object>(ad.rawQuery("SELECT currval(\'sqarma\');")); // get the id
        }
        
        if (data != null && data.size() > 0) {
            ArrayList<Object> row = (ArrayList<Object>) data.get(0);
            Double temp = Double.parseDouble(row.get(0).toString());
            this.id = temp.intValue();
            return true;
        }
        return false;
    }
    
    /**
     * Updates the database with the values of this instance
     * @return true if more than 0 rows are affected by this operation
     * @throws SQLException if some exception related with the database happend
     * @throws Exception if some other exception happend during the excecution of this operation happend
     */
     public boolean update() throws SQLException, Exception {
        int rowsAffected;
        Map<String, Object> values = new LinkedHashMap<String, Object>();
        values.put(NOMBRE, getNombre());
        values.put(CALIBRE, getCalibre());
        values.put(MARCA, getMarcar());
        values.put(MODELO, getModelo());
        values.put(MATRICULA, getMatricula());
        values.put(ID_OFICIAL, getIdOficial());
        
        Map<String,Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        
        rowsAffected = caracolQuery.update(TABLE, values, conditions);
        
        return rowsAffected > 0;
    }
     
     /**
      * Deletes the entity from the database where it contains the indentifier of this instance
      * @return true if this entity is delete otherwise false
      * @throws SQLException if some database exception happend during the excecution of the operation
      * @throws Exception if some exception happend during the excecution of the operation
      */
     public boolean delete() throws SQLException, Exception {
        if (id <= 0) { // this is already delete
            return true;
        }

        Map<String, Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        int rowsAffected = caracolQuery.delete(TABLE, conditions);

        if (rowsAffected > 0) {
            id = 0;
        }

        return rowsAffected > 0;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @return the marcar
     */
    public String getMarcar() {
        return marcar;
    }

    /**
     * @param marcar the marcar to set
     */
    public void setMarcar(String marcar) {
        this.marcar = marcar;
    }

    /**
     * @return the calibre
     */
    public String getCalibre() {
        return calibre;
    }

    /**
     * @param calibre the calibre to set
     */
    public void setCalibre(String calibre) {
        this.calibre = calibre;
    }

    /**
     * @return the modelo
     */
    public String getModelo() {
        return modelo;
    }

    /**
     * @param modelo the modelo to set
     */
    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    /**
     * @return the matricula
     */
    public String getMatricula() {
        return matricula;
    }

    /**
     * @param matricula the matricula to set
     */
    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    /**
     * @return the idOficial
     */
    public int getIdOficial() {
        return idOficial;
    }

    /**
     * @param idOficial the idOficial to set
     */
    public void setIdOficial(int idOficial) {
        this.idOficial = idOficial;
    }
    
}
